//each node in the tree is an Array with 4+n positions
// node[0] is 0/1 when the node is closed/open
// node[1] is 0/1 when the folder is closed/open
// node[2] is 1 if the children of the node are documents
// node[3] is the name of the folder
// node[4]...node[4+n] are the n children nodes

// ***************
// Building the data in the tree

function logout() {
	if (confirm('If you want to logout this session, please press "OK" and make sure this configuration window is closed.')) {
		location = "logout.asp";
	}
}

function generateTree() {
	var aux1, aux2, aux3, aux4;

	mode = getMode();

	foldersTree = folderNode(mode + 'Setting');

	aux1 = appendChild(foldersTree, leafNode("Status & Log"));
	appendChild(aux1, generateDocEntry(0, "Status", "status.asp", ""));
	appendChild(aux1, generateDocEntry(0, "WiFi", "wifistatus.asp", ""));
	if (mode != 'AP') {
		if (hasUSB()) {
			if (rt_config & CONFIG_MADWIMAX)
				appendChild(aux1, generateDocEntry(0, "WiMAX", "wimaxstatus.asp", ""));
			if (rt_config & CONFIG_MODEM) {
				appendChild(aux1, generateDocEntry(0, "3G/CDMA Modem", "modemstatus.asp", ""));
				appendChild(aux1, generateDocEntry(0, "Modem Log", "chatstatus.asp", ""));
			}
		}
		appendChild(aux1, generateDocEntry(0, "DHCP Leases", "dhcpstatus.asp", ""));
		appendChild(aux1, generateDocEntry(0, "Port Forwarding", "iptstatus.asp", ""));
		appendChild(aux1, generateDocEntry(0, "Routing Table", "routestatus.asp", ""));
		appendChild(aux1, generateDocEntry(0, "System Utilization", "utilization.asp", ""));
		appendChild(aux1, generateDocEntry(0, "Diagnostic Information", "sysinfo.asp", ""));
	}
	appendChild(aux1, generateDocEntry(0, "System Log", "logstatus.asp", ""));

	// aux1 = appendChild(foldersTree, leafNode("Quick Setup"));
	aux1 = appendChild(foldersTree, leafNode("Wi-Fi"));
	appendChild(aux1, generateDocEntry(0, "Basic Settings", "wifi.asp", ""));
	appendChild(aux1, generateDocEntry(0, "Bridge", "wmode.asp", ""));
	appendChild(aux1, generateDocEntry(0, "Access Control", "acl.asp", ""));
	appendChild(aux1, generateDocEntry(0, "RADIUS Settings", "wsecurity.asp", ""));
	appendChild(aux1, generateDocEntry(0, "Advanced Settings", "wifiadv.asp", ""));

	if (mode == 'AP') {
		aux1 = appendChild(foldersTree, leafNode("IP Config"));
		appendChild(aux1, generateDocEntry(0, "LAN", "aplan.asp", ""));
		if (rt_config & CONFIG_IPV6)
			appendChild(aux1, generateDocEntry(0, "IPv6", "ipv6.asp", ""));
		if (rt_config & CONFIG_SNMP)
			appendChild(aux1, generateDocEntry(0, "SNMP", "snmp.asp", ""));
		appendChild(aux1, generateDocEntry(0, "Miscellaneous", "rmisc.asp", ""));
	} else {
		aux1 = appendChild(foldersTree, leafNode("IP Config"));
		appendChild(aux1, generateDocEntry(0, "WAN", "wan.asp", ""));
		appendChild(aux1, generateDocEntry(0, "LAN", "lan.asp", ""));
		if (rt_config & CONFIG_IPV6)
			appendChild(aux1, generateDocEntry(0, "IPv6", "ipv6.asp", ""));
		if (rt_config & CONFIG_SNMP)
			appendChild(aux1, generateDocEntry(0, "SNMP", "snmp.asp", ""));
		appendChild(aux1, generateDocEntry(0, "DHCP Server", "dhcp.asp", ""));
		appendChild(aux1, generateDocEntry(0, "Route", "gwstaticroute.asp", ""));
		appendChild(aux1, generateDocEntry(0, "Miscellaneous", "ddns.asp", ""));

		if (mode == 'Gateway') {
			aux1 = appendChild(foldersTree, leafNode("NAT Settings"));
			appendChild(aux1, generateDocEntry(0, "Port Trigger", "porttrigger.asp", ""));
			appendChild(aux1, generateDocEntry(0, "Virtual Server", "virtualserver.asp", ""));
			appendChild(aux1, generateDocEntry(0, "Virtual DMZ", "exposed.asp", ""));
		}
		
		aux1 = appendChild(foldersTree, leafNode("Internet Firewall"));
		appendChild(aux1, generateDocEntry(0, "Basic Config", "basicfirewall.asp", ""));
		appendChild(aux1, generateDocEntry(0, "WAN &amp; LAN Filter", "firewall.asp", ""));
		appendChild(aux1, generateDocEntry(0, "URL Filter", "urlfilter.asp", ""));
		appendChild(aux1, generateDocEntry(0, "MAC Filter", "macfilter.asp", ""));
	}

	if (mode != 'AP') {
		aux1 = appendChild(foldersTree, leafNode("Bandwidth Management"));
		appendChild(aux1, generateDocEntry(0, "Basic Config", "qos.asp", ""));
	}

	if (hasUSB()) {
		if (rt_config & (CONFIG_VSFTPD | CONFIG_SAMBA | CONFIG_NFSD | CONFIG_MJPG)) {
			aux1 = appendChild(foldersTree, leafNode("USB Application"));
			if (rt_config & CONFIG_VSFTPD)
				appendChild(aux1, generateDocEntry(0, "FTP Server", "usbstorage.asp", ""));
			if (rt_config & CONFIG_SAMBA)
				appendChild(aux1, generateDocEntry(0, "Samba", "samba.asp", ""));
			if (rt_config & CONFIG_NFSD)
				appendChild(aux1, generateDocEntry(0, "NFS Server", "nfs.asp", ""));
			if (rt_config & CONFIG_MJPG)
				appendChild(aux1, generateDocEntry(0, "Web Camera", "webcam.asp", ""));
		}
		
		if (rt_config & (CONFIG_MODEM | CONFIG_MADWIMAX)) {
			aux1 = appendChild(foldersTree, leafNode("USB Network Devices"));
			if (rt_config & CONFIG_MODEM)
				appendChild(aux1, generateDocEntry(0, "3G/CDMA Modem", "connectionmode.asp", ""));
			if (rt_config & CONFIG_MADWIMAX)
				appendChild(aux1, generateDocEntry(0, "WiMAX", "wimax.asp", ""));
		}
	}

	aux1 = appendChild(foldersTree, leafNode("System Setup"));
	appendChild(aux1, generateDocEntry(0, "Operation Mode", "operationmode.asp", ""));
	appendChild(aux1, generateDocEntry(0, "Services Configuration", "services.asp", ""));
	if (rt_config & CONFIG_MODEM)
		appendChild(aux1, generateDocEntry(0, "3G/CDMA Modem", "modem.asp", ""));
	appendChild(aux1, generateDocEntry(0, "Change Name", "name.asp", ""));
	appendChild(aux1, generateDocEntry(0, "Change Password", "password.asp", ""));
	appendChild(aux1, generateDocEntry(0, "Firmware Update", "fwupdate.asp", ""));
	appendChild(aux1, generateDocEntry(0, "Settings Management", "settingbackup.asp", ""));
	appendChild(aux1, generateDocEntry(0, "Flashfs Management", "flashfsbackup.asp", ""));
	appendChild(aux1, generateDocEntry(0, "Factory Default", "factorydefault.asp", ""));
	appendChild(aux1, generateDocEntry(0, "System Command", "syscmd.asp", ""));
	
	aux1 = appendChild(foldersTree, leafNode("Logout"));
}

function clickOnFolderLink(folderName) {
	mode = getMode();

	if (folderName == "Status & Log") {
		top.folderFrame.location = "status.asp";
	} else if (folderName == "Logout") {
		logout();
	// } else if (folderName == "Quick Setup") {
	//	if (mode == 'AP')
	//		top.folderFrame.location = "aoperation.asp";
	//	else if (mode == 'Router')
	//		top.folderFrame.location = "goperation.asp";
	//	else
	//		top.folderFrame.location = "goperation.asp";
	} else if (folderName == "Wi-Fi") {

		top.folderFrame.location = "wifi.asp";

	} else if (folderName == "IP Config") {
		if (mode == 'AP')
			top.folderFrame.location = "aplan.asp";
		else
			top.folderFrame.location = "wan.asp";
	} else if (folderName == "Internet Firewall") {
		top.folderFrame.location = "basicfirewall.asp";
	} else if (folderName == "USB Application") {
		top.folderFrame.location = "usbstorage.asp";
	} else if (folderName == "Bandwidth Management") {
		top.folderFrame.location = "qos.asp";
	} else if (folderName == "NAT Setting") {
		top.folderFrame.location = "porttrigger.asp";
	} else if (folderName == "System Setup") {
		top.folderFrame.location = "operationmode.asp";
	} else if (folderName == "USB Network Devices") {
		if (rt_config & CONFIG_MODEM)
			top.folderFrame.location = "connectionmode.asp";
		else
			top.folderFrame.location = "wimax.asp";
	}
}

// Auxiliary function to build the node
function folderNode(name) {
	var arrayAux;

	arrayAux = new Array;
	arrayAux[0] = 1;
	arrayAux[1] = 1;
	arrayAux[2] = 0;
	arrayAux[3] = name;

	return arrayAux;
}

// Auxiliary function to build the node
// The entries in arrayAux[4]..array[length-1] are strings built in generate doc
// entry
function leafNode(name) {
	var arrayAux;
	arrayAux = new Array;
	arrayAux[0] = 0;
	arrayAux[1] = 0;
	arrayAux[2] = 1;
	arrayAux[3] = name;

	return arrayAux;
}

// this way the generate tree function becomes simpler and less error prone
function appendChild(parent, child) {
	parent[parent.length] = child;
	return child;
}

// these are the last entries in the hierarchy, the local and remote links to
// html documents
function generateDocEntry(icon, docDescription, link) {
	var retString = "";
	var szDoc = "";
	if (icon === 0) {
		retString = "<a href='" +
				link +
				"' class='wounderline' onClick='return top.checkChanged()' target=folderFrame><img src='./img/doc.gif' alt='Opens in right frame'";
		szDoc = "<a href='" +
				link +
				"' class='wounderline' onClick='return top.checkChanged()' target=folderFrame>" +
				docDescription + "</a>";
	} else {
		retString = "<a href='http://" +
				link +
				"' class='wounderline' onClick='return top.checkChanged()' target=_blank><img src='./img/link.gif' alt='Opens in new window'";
		szDoc = "<a href='" +
				link +
				"' class='wounderline' onClick='return top.checkChanged()' target=_blank>" +
				docDescription + "</a>";
	}
	retString = retString +
			" border=0></a><td nowrap><font size=-1 face='Arial, Helvetica'>" +
			szDoc + "</font>";

	return retString;
}

// **********************
// display functions

// redraws the left frame
function redrawTree() {
	var doc = top.treeFrame.window.document;

	mode = getMode();

	doc.write("<html>");
	doc.write("<head>");
	doc.write("<link rel='stylesheet' type='text/css' href='style.css' media='screen'>");
	doc.write("</head>");
	doc.write("<body bgcolor='#5b86ba' text='#FFFFFF'>");
	doc.write("<table width='120' border='0' cellspacing='0' cellpadding='0' height='125'>");
	doc.write("<tr>");
	doc.write("<td>");
	doc.write("<div align='center'>");
	doc.write("<img src='img/logo.png' width='150' height='50'></div>");

	doc.write("<div align='center'>");
	if (mode == 'AP')
		doc.write("<span style='font-family: Verdana; font-size: 10pt; font-weight:bold; color:#ffffff'>Access Point</span>");
	else if (mode == 'Gateway')
		doc.write("<span style='font-family: Verdana; font-size: 10pt; font-weight:bold; color:#ffffff'>Home Gateway</span>");
	else if (mode == 'Router')
		doc.write("<span style='font-family: Verdana; font-size: 10pt; font-weight:bold; color:#ffffff'>Router</span>");
	doc.write("</div>");

	doc.write("</td>");
	doc.write("</tr>");
	doc.write("</table>");
	redrawNode(foldersTree, doc, 0, 1, "");
	doc.close();
}

// recursive function over the tree structure called by redrawTree
function redrawNode(foldersNode, doc, level, lastNode, leftSide) {
	var j = 0;
	var i = 0;

	if (level > 0) {
		doc.write("<table border=0 cellspacing=0 cellpadding=0>");
		doc.write("<tr><td valign='middle' nowrap>");

		doc.write(leftSide);

		if (level > 1){
			if (lastNode) // the last 'brother' in the children array
			{
				doc.write("<img src='./img/lastnode.gif' width=16 height=22>");
				leftSide = leftSide + "<img src='./img/blank.gif' width=16 height=22>";
			} else {
				doc.write("<img src='./img/node.gif' width=16 height=22>");
				leftSide = leftSide +
						"<img src='./img/vertline.gif' width=16 height=22>";
			}
		}
		displayIconAndLabel(foldersNode, doc);
		doc.write("</table>");
	}

	if (foldersNode.length > 4 && foldersNode[0]) // there are sub-nodes and
													// the folder is open
	{
		if (!foldersNode[2]) // for folders with folders
		{
			level = level + 1;
			for (i = 4; i < foldersNode.length; i++){
				if (i == foldersNode.length - 1)
					redrawNode(foldersNode[i], doc, level, 1, leftSide);
				else
					redrawNode(foldersNode[i], doc, level, 0, leftSide);
			}
		} else // for folders with documents
		{
			for (i = 4; i < foldersNode.length; i++) {
				doc.write("<table border=0 cellspacing=0 cellpadding=0 valign=center>");
				doc.write("<tr><td nowrap>");
				doc.write(leftSide);
				if (i == foldersNode.length - 1)
					doc.write("<img src='./img/lastnode.gif' width=16 height=22>");
				else
					doc.write("<img src='./img/node.gif' width=16 height=22>");
				doc.write(foldersNode[i]);
				doc.write("</table>");
			}
		}
	}
}

// builds the html code to display a folder and its label
function displayIconAndLabel(foldersNode, doc) {
	var szOpenIconName = "./img/openfolder.gif";
	var szCloseIconName = "./img/closedfolder.gif";
	doc.write("<a href='javascript:top.openBranch(\"" + foldersNode[3] +
			"\")' onClick='return top.checkChanged()'><img src=");
	if (foldersNode[1])
		doc.write(szOpenIconName + " width=22 height=22 border=noborder></a>");
	else
		doc.write(szCloseIconName + " width=22 height=22 border=noborder></a>");

	doc.write("<td valign=middle align=left nowrap>");
	doc.write("<a href='javascript:top.openBranch(\"" + foldersNode[3] +
			"\")' class='wounderline' onClick='return top.checkChanged()'>");
	doc.write("<font size=-1 face='Arial, Helvetica'>" + foldersNode[3] +
			"</font></a>");
}

// **********************+
// Recursive functions

// when a parent is closed all children also are
function closeFolders(foldersNode) {
	var i = 0;

	if (!foldersNode[2]) {
		for (i = 4; i < foldersNode.length; i++)
			closeFolders(foldersNode[i]);
	}
	foldersNode[0] = 0;
	foldersNode[1] = 0;
}

// recursive over the tree structure
// called by openbranch
function clickOnFolderRec(foldersNode, folderName) {
	var i = 0;

	if (foldersNode[3] == folderName) {

		if (foldersNode[0]) {
			closeFolders(foldersNode);
		} else {
			clickOnFolderLink(folderName);
			if (//folderName != 'Quick Setup' &&
					folderName != 'Logout') {
				foldersNode[0] = 1;
				foldersNode[1] = 1;
			} else {
				// closeFolders(foldersTree)
				initializeTree();
			}
		}
	} else {
		if (!foldersNode[2]) {
			for (i = 4; i < foldersNode.length; i++) {
				clickOnFolderRec(foldersNode[i], folderName);
			}
		}
	}
}

// ********************
// Event handlers

//called when the user clicks on a folder
function openBranch(branchName) {
	clickOnFolderRec(foldersTree, branchName);
	timeOutId = setTimeout("redrawTree()", 100);
}

//called after this html file is loaded
function initializeTree() {
	foldersTree = 0;
	generateTree();
	redrawTree();
}

function checkChanged() {
	if (pageChanged == '1') {
		if (pageChangedCount == 6) {
			pageChangedCount = 0;
			ret = 1;
		} else if (pageChangedCount > 2) {
			ret = confirm('Please, please, please press "Apply" or "Finish" to confirm setting if you DO NOT ignore your changes!!!!!');
		} else
			ret = confirm('Page has been modified, are you sure to ignore changes?');

		if (ret) {
			pageChanged = 0;
			pageChangedCount = 0;
		} else {
			pageChangedCount++;
		}

		return ret;
	} else
		return true;
}

var foldersTree = 0;
var timeOutId = 0;
var pageChanged = 0;
var pageChangedCount = 0;
var goquick = 0;
